package com.example.design.ddd.practice2.domain.Payment.valueobject;

import java.time.LocalDateTime;

// 支付结果值对象（不可变设计）
public final class PaymentResult {
    private final boolean success;
    private final String transactionId; // 第三方支付交易号
    private final String errorCode;
    private final String errorMessage;
    private final LocalDateTime completeTime;

    // 成功结果构造方法
    public PaymentResult(String transactionId) {
        this.success = true;
        this.transactionId = transactionId;
        this.errorCode = null;
        this.errorMessage = null;
        this.completeTime = LocalDateTime.now();
    }

    // 失败结果构造方法
    public PaymentResult(String errorCode, String errorMessage) {
        this.success = false;
        this.transactionId = null;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.completeTime = LocalDateTime.now();
    }

    // 领域方法 - 验证结果有效性
    public void validate() {
        if (success && (transactionId == null || transactionId.isEmpty())) {
            throw new PaymentVerificationException("INVALID_TRANSACTION_ID");
        }
        if (!success && (errorCode == null || errorCode.isEmpty())) {
            throw new PaymentVerificationException("MISSING_ERROR_CODE");
        }
    }

    public Boolean isSuccess() {
        return success;
    }

    // getters...
}
